package com.example.service;

import com.example.common.Result;
import com.example.mapper.LoginMapper;
import com.example.pojo.Emp;
import com.example.pojo.LoginInfo;
import com.example.utils.JwtUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class LoginService {

    @Autowired
    private LoginMapper loginMapper;

    public LoginInfo login(Emp emp) {
        Emp e = loginMapper.login(emp.getUsername(), emp.getPassword());
        //2. 判断: 判断是否存在这个员工, 如果存在, 组装登录成功信息
        if (e != null) {
//            log.info("登录成功, 员工信息: {}", e);
            //生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);

            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), jwt);

        }
        else {
//            log.info("登录失败, 员工信息: {}", e);
            return null;
        }
    }
}
